import { defineStore } from 'pinia';
import { setStorageSync } from '../../storage';
import { store } from '@/store/pinia'
// import { addLog } from '@/plugins/api/log'

const errorState = {
    system: {},
    error: {}
}

export const errorLog = defineStore('errorLog', {
    state: () => errorState,
    actions: {
        setSystemInfo(data: object) {
            this.$state.system = data;
            setStorageSync('system', data)
        },
        async setErrorLogInfo(data: object | any) {
            let errRouter = 'App.vue'
            if(getCurrentPages().length) {
                // @ts-ignore
                errRouter = getCurrentPages()[0].route
            }
            const {
                system,
                windowWidth,
                windowBottom,
                windowTop,
                platform,
                windowHeight,
                deviceId,
                model,
                language
            }: any = this.$state.system;
            const err = {
                errorData: `${data.info}：${data.err.message}`,
                routerClient: errRouter,
                systemClient: system,
                platform: platform,
                deviceId: deviceId,
                language: language,
                windowTop: windowTop,
                windowBottom: windowBottom,
                windowHeight: windowHeight,
                windowWidth: windowWidth,
                model: model,
                code: data.code? data.code : null,
                requestMethods: data.requestMethods || null,
                params: JSON.stringify(data.params) || null,
                routerService: data.routerService || null,
            }
            console.log(err)
            // await addLog(err)
            this.$state.error = err;
        }
    },
    getters: {
        gettersSetSystem: state => state.system,
        gettersSetErrorLog: state => state.error
    }
})
export const useErrorLog = () => errorLog(store);